var gulp = require('gulp'),
	sass = require('gulp-sass'),
	cleancss = require('gulp-clean-css'),
	uglify = require('gulp-uglify'),
	clean = require('gulp-clean'),
	concat = require('gulp-concat'),
	header = require('gulp-header'),
	sourcemaps = require('gulp-sourcemaps'),
	notify = require('gulp-notify'),
	plumber = require('gulp-plumber'),
	rev = require('gulp-rev'),
	revcollector = require('gulp-rev-collector'),
	replace = require('gulp-replace'),
	rename = require('gulp-rename'),
	fileinclude = require('gulp-file-include'),
	livereload = require('gulp-livereload');

var path = require('path'),
	fs = require("fs");

var pkg = require('./package.json'),
	notes = ['/**',
	  ' * @author <%= pkg.author.name %>',
	  ' * @email <%= pkg.author.email %>',
	  ' * @descrip <%= pkg.description %>',
	  ' * @version v<%= pkg.version %>',
	  ' */',
	  ''].join('\n');

gulp.task('sass', function() {
	return gulp.src('dev/style/**/*.scss')
			.pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
			.pipe(sass())
			.pipe(cleancss())
			.pipe(header(notes, { pkg : pkg } ))
			.pipe(rename('style.css'))
			.pipe(gulp.dest('./'))
			.pipe(livereload());
});

gulp.task('css', function(){
	return gulp.src('dev/style/**/*.css')
			.pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
			.pipe(cleancss())
			.pipe(gulp.dest('./css'))
			.pipe(livereload());
});

gulp.task('uglifyjs', function() {
	return gulp.src(['dev/script/**/*.js', '!dev/script/**/*.min.js'])
			.pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
			.pipe(uglify())
			.pipe(header(notes, { pkg : pkg } ))
			.pipe(gulp.dest('js'))
			.pipe(livereload());
});

gulp.task('js', function(){
	return gulp.src('dev/script/**/*.min.js')
			.pipe(gulp.dest('js'))
			.pipe(livereload());
});

gulp.task('default', ['sass','css','uglifyjs','js'], function(){
    
});

gulp.task('watch', function() {
    gulp.watch('dev/style/**/*.scss', ['sass']);
    gulp.watch('dev/style/**/*.css', ['css']);
    gulp.watch(['dev/script/**/*.js', '!dev/script/**/*.min.js'], ['uglifyjs']);
    gulp.watch('dev/script/**/*.min.js', ['js']);
});

